<?php

declare (strict_types=1);

namespace app\store\controller\agent;

use app\store\controller\Controller;
use think\App;
use think\response\Json;
use app\store\service\agent\Channel as ChannelService;

/**
 * 渠道管理
 * Class Setting
 * @package app\store\controller
 */
class Channel extends Controller
{
    private $channelService;
    public function __construct(App $app,ChannelService $channelService)
    {
        parent::__construct($app);
        $this->channelService = $channelService;
    }

    /**
     * 渠道列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer'
            ]
        );
        $list = $this->channelService->getList($params);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加、修改渠道
     * @return array|mixed
     */
    public function save():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'name|渠道名称' => 'require',
                'divide|返利比例' => 'require|integer',
                'sort|排序' => 'integer',
                'divide_type|返利比例方式' => 'require|integer',
                'username|账号' => 'require',
            ]
        );
        if(empty($params['id'])){
            //新增
            $this->validate(
                $params,
                [
                    'mobile|手机号码' => 'require',
                    'name|渠道名称' => 'require',
                    'divide|返利比例' => 'require|integer',
                    'sort|排序' => 'integer',
                    'divide_type|返利比例方式' => 'integer',
                    'username|账号' => 'require',
                    'password|密码' => 'require',
                    'confirm_password|确认密码' => 'require',
                ]
            );
        }
        //用户名重复校验
        $review_username_check = $this->channelService->getReviewUsernameCheck($params['username'],$params['id'] ?? '');
        if(!$review_username_check){
            return $this->renderError('用户名重复');
        }
        // 保存记录
        $res = $this->channelService->save($params);
        if (!$res) {
            return $this->renderError('保存失败');
        }
        return $this->renderSuccess('保存成功');
    }


    /**
     * 删除渠道
     * @param int $categoryId
     * @return array
     * @throws \Exception
     */
    public function delete():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            ['id|渠道id' => 'require|integer']
        );
        $res = $this->channelService->remove($params['id']);
        if (!$res) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**冻结操作
     * @return Json
     */
    public function frozenWithDraw():Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id|渠道id' => 'require|integer',
                'is_with_draw' => 'require|integer',
            ]
        );
        $res = $this->channelService->getFrozenWithDraw($params);
        if (!$res) {
            return $this->renderError( '操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }

}
